package com.sm.user.rocket_mq_consumer;

import com.sm.tool.entity.dto.SentTicketDTO;
import com.sm.tool.entity.dto.SentTicketsDTO;
import com.sm.tool.entity.po.Tickets_;
import com.sm.tool.entity.po.UserTicket;
import com.sm.user.mapper.UserTicketMapper;
import groovy.util.logging.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @program: practice
 * @description:
 * @author: liqitian.
 * @create: 2019-11-28 17:42
 **/
@Service
@Slf4j
@RocketMQMessageListener(consumerGroup = "sm-get_ticket-topic_consumer", topic = "sm_sent_ticket_topic")
public class GetTicketConsumer implements RocketMQListener<SentTicketsDTO> {

    @Autowired
    private UserTicketMapper userTicketMapper;

    @Override
    public void onMessage(SentTicketsDTO sentTicketsDTO) {

        System.out.println("sm-get_ticket-topic_consumer,consumer_get_ticket-topic 接收数据:top:sentTicketsDTO:"+sentTicketsDTO);
        Long userId = sentTicketsDTO.getUserId();
        List<SentTicketDTO> sentTicketDTOList = sentTicketsDTO.getSentTicketDTOList();

        for (SentTicketDTO sentTicketDTO : sentTicketDTOList) {
            Tickets_ tickets_ = sentTicketDTO.getTickets_();
            int number = sentTicketDTO.getNumber();

            for (int i = 0; i < number; i++) {
                UserTicket userTicket = UserTicket.builder().build();

                userTicket.setUserId(userId);
                userTicket.setTicketId(tickets_.getId());
                userTicket.setCollectTime(new Date());
                userTicket.setCreateTime(new Date());

                Calendar c = Calendar.getInstance();
                //计算x天后的时间
                c.add(Calendar.DATE, tickets_.getTermOfValidity());
                userTicket.setExpriceTime(c.getTime());
                userTicketMapper.insert(userTicket);
            }

        }


    }
}
